package com.caixin.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
*SwaggerConfig
*
* @author caixin
* @version 1.0.0
* @date 2021年12月11日 20:27
*/
@EnableSwagger2
@Configuration
@EnableSwaggerBootstrapUI
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //是否开启Swagger
                .enable(true)
                //用来创建该api的基本信息
                .apiInfo(createApiInfo())
                //select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
                .select()
                //扫描当前包路径,所有Controller所在的包路径
                .apis(RequestHandlerSelectors.basePackage("com.caixin.api"))
                //定义要生成文档的Api的url路径规则
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数
     *
     * @return springfox.documentation.service.ApiInfo
     * @date 2021年12月11日 20:26
     */
    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("XXXXX服务接口API文档")
                //创建人
                .contact(new Contact("创建人名称", "公司主页", "邮箱"))
                //版本号
                .version("1.0")
                //描述信息
                .description("API描述")
                .build();
    }



    /**
     * 解决静态资源无法访问的问题
     *
     * @param registry:
     * @return void
     * @date 2021年12月11日 20:27
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 解决swagger死循环问题
     * 视具体情况用,若控制台报错 No mapping for GET /aifruit/null/swagger-resources/configuration/security
     * 则使用下面代码
     * 下面这几个地址会一直轮询，改为重定向
     * /null/swagger-resources/configuration/ui
     * /null/swagger-resources/configuration/security
     * /null/swagger-resources
     */
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addRedirectViewController("/null/api-docs",
//                "/api-docs").setKeepQueryParams(true);
//        registry.addRedirectViewController("/null/swagger-resources/configuration/ui",
//                "/swagger-resources/configuration/ui");
//        registry.addRedirectViewController("/null/swagger-resources/configuration/security",
//                "/swagger-resources/configuration/security");
//        registry.addRedirectViewController("/null/swagger-resources", "/swagger-resources");
//    }

}
